
import 'package:flutter/material.dart';
import 'package:connectivity/connectivity.dart';
class NetWorkPage extends StatefulWidget {
  NetWorkPage({Key key}) : super(key: key);

  @override
  _NetWorkPageState createState() => _NetWorkPageState();
}

class _NetWorkPageState extends State<NetWorkPage> {
  var subscription;
  String networkString = '';

  @override
  void initState() {
    super.initState();
    //监听网络的变化
    subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
      if(result == ConnectivityResult.wifi){
          setState(() {
            this.networkString = "WIFI网络";
          });
      }else if(result == ConnectivityResult.mobile){
         setState(() {
            this.networkString = "手机网络";
          });
      }else{
        setState(() {
            this.networkString = "没有网络";
          });
      }
    });
  }
  @override
  dispose() {
    super.dispose();

    subscription.cancel();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("网络检查"),
      ),
      body: Center(
        child: Text(
          this.networkString.isNotEmpty ? this.networkString : ''
        ),
      ),
    );
  }
}
